//
//  WhzHomeCollectionViewController.m
//  WhzMT
//
//  Created by whz on 16/12/9.
//  Copyright © 2016年 whz. All rights reserved.
//

#import "WhzHomeCollectionViewController.h"
#import "Header.h"
#import "WhzSortView.h"
#import "WhzCategoryViewController.h"
#import "WhzDistrictViewController.h"

#define CELLID @"cellID"

@interface WhzHomeCollectionViewController ()

@end

@implementation WhzHomeCollectionViewController

#pragma mark - 初始化UICollectionViewFlowLayout

- (instancetype)init {

    if (self = [super initWithCollectionViewLayout:[UICollectionViewFlowLayout new]]) {
        
        // 设置颜色
        self.collectionView.backgroundColor = WhzColorRGB(231, 231, 231);
        
        // 设置导航栏
        [self setupNav];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CELLID];
    
    // Do any additional setup after loading the view.
}

#pragma mark - 导航栏相关

- (void)setupNav {

    // 设置右侧 Item
    UIBarButtonItem *itemMap = [UIBarButtonItem barButtonItemWithTarget:self action:@selector(btnClickMapItem) icon:@"icon_map" highLightIcon:@"icon_map_highlighted"];
    UIBarButtonItem *itemSearch = [UIBarButtonItem barButtonItemWithTarget:self action:@selector(btnClickSearchItem) icon:@"icon_search" highLightIcon:@"icon_search_highlighted"];
    self.navigationItem.rightBarButtonItems = @[itemMap, itemSearch];
    
    // 设置左侧 Item
    UIBarButtonItem *itemLogo = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"icon_meituan_logo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:nil action:nil];
    // 设置禁用
    itemLogo.enabled = NO;
    
    // 设置分类 Item
    UIBarButtonItem *categoryItem = [UIBarButtonItem barButtonItemWithView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 40)] Target:self action:@selector(btnClickCategory) icon:@"icon_category_-1" highLightIcon:@"icon_category_highlighted_-1" title1:@"全部分类" title2:nil];
    
    
    // 设置区域 Item
    UIBarButtonItem *districtItem = [UIBarButtonItem barButtonItemWithView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 40)] Target:self action:@selector(btnClickDistrict) icon:@"icon_district" highLightIcon:@"icon_district_highlighted" title1:@"北京-全部" title2:nil];
    
    // 设置排序 Item
//    UIBarButtonItem *sortItem = [UIBarButtonItem barButtonItemWithView:[[WhzSortView alloc] initWithFrame:CGRectMake(0, 0, 150, 40)] Target:self action:@selector(btnClickSort) icon:@"icon_sort" highLightIcon:@"icon_sort_highlighted" title1:@"排序" title2:@"默认排序"];
    
    
    WhzSortView *sortView = [WhzSortView new];
    UIBarButtonItem *sortItem = [[UIBarButtonItem alloc] initWithCustomView:sortView];
    [sortView addTarget:self action:@selector(btnClickSort) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItems = @[itemLogo, categoryItem, districtItem, sortItem];
}

#pragma mark - 监听事件

- (void)btnClickMapItem {

    NSLog(@"点击地图");
}

- (void)btnClickSearchItem {

    NSLog(@"点击搜索");
}

- (void)btnClickCategory {

    NSLog(@"点击分类");
    
    WhzCategoryViewController *categoryVc = [[WhzCategoryViewController alloc] init];
    // 设置 Modal 类型 Popover
    categoryVc.modalPresentationStyle = UIModalPresentationPopover;
    // 设置 barButtonItem
    UIPopoverPresentationController *popover = categoryVc.popoverPresentationController;
    popover.barButtonItem = self.navigationItem.leftBarButtonItems[1];
    // 设置 Popover 的大小
    categoryVc.preferredContentSize = CGSizeMake(categoryVc.popoverView.width, categoryVc.popoverView.height);
    
    [self presentViewController:categoryVc animated:YES completion:nil];
}

- (void)btnClickDistrict {

    NSLog(@"点击区域");
    
    WhzDistrictViewController *districtVc = [[WhzDistrictViewController alloc] init];
    districtVc.modalPresentationStyle = UIModalPresentationPopover;
    districtVc.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItems[2];
    
    districtVc.preferredContentSize = CGSizeMake(districtVc.districtPopoverView.width, CGRectGetMaxY(districtVc.districtPopoverView.frame));
    
    [self presentViewController:districtVc animated:YES completion:nil];
}

- (void)btnClickSort {

    NSLog(@"点击排序");
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 0;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELLID forIndexPath:indexPath];
    
    
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
